package org.leumasjaffe.math.probability;

import java.util.Random;

import org.eclipse.jdt.annotation.NonNull;

import flanagan.analysis.Stat;


public class ChiSquaredRV extends AbstractRV {
	final int k;

	public ChiSquaredRV(int k) {
		this.k = k;
	}

	public ChiSquaredRV(@NonNull Random r, int k) {
		super(r);
		this.k = k;
	}

	@Override
	public double getEV() {
		return this.k;
	}

	@Override
	public double getVar() {
		return 2*this.k;
	}

	@Override
	public double getP(double x) {
		return Stat.chiSquarePDF(x, this.k);
	}

	@Override
	public double calcX(double p) {
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		return Stat.chiSquareInverseCDF(this.k, p);
	}

	@Override
	public double getF(double x) {
		return Stat.regularisedGammaFunction((double) this.k/2, x/2);
	}

}
